﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Weather.Base;

namespace Gismeteo3
{
    internal class ForecastInfoList
    {
        public List<ForecastInfo> List = new List<ForecastInfo>();

        public ForecastData getForecast(DateTime Date)
        {
            String Cloud = String.Empty;
            String Precipitation = String.Empty;
            String Thunderstorm = String.Empty;
            Int32 Cl = 0;
            Int32 PrT = 0;
            Int32 Pr = 0;
            Boolean Th = false;
            Int32 tMin = Int32.MaxValue;
            Int32 tMax = Int32.MinValue;

            foreach (ForecastInfo fi in List.Where(p => p.dt >= Date && p.dt < Date.AddDays(1)))
            {
                if (String.IsNullOrEmpty(Cloud)) Cloud = fi.Cloud;
                if (Cl < fi.Cl) { Cl = fi.Cl; Cloud = fi.Cloud; }
                if (Pr < fi.Pr) { Pr = fi.Pr; Precipitation = fi.Precipitation; PrT = fi.PrT; }
                if (fi.Th) { Th = true; Thunderstorm = fi.Thunderstorm; }
                if (tMin > fi.temp) tMin = fi.temp;
                if (tMax < fi.temp) tMax = fi.temp;
            }
            ForecastInfo fi0 = new ForecastInfo("d.sun" + (Cl > 0 ? ".c" + Cl : String.Empty) + (Pr > 0 ? "." + (PrT == 1 ? "r" : "s") + Pr.ToString() : String.Empty) + (Th ? ".st" : String.Empty));
            return new ForecastData()
            {
                HighTemperature = tMax,
                LowTemperature = tMin,
                Text = Cloud + (!String.IsNullOrEmpty(Precipitation) ? ", " + Precipitation : String.Empty) + (!String.IsNullOrEmpty(Thunderstorm) ? ", " + Thunderstorm : String.Empty),
                SkyCode = fi0.SkyCode,
                Url = String.Empty
            };


            //for (DateTime dc = DateTime.Today; dc < DateTime.Today.AddDays(5); dc = dc.AddDays(1))
            //{
            //    ForecastData fd = new ForecastData() { HighTemperature = Int32.MinValue, LowTemperature = Int32.MaxValue };
            //    foreach (ForecastImageComparer tfm in fListMain)
            //        if (tfm.dt == dc)
            //            fd.SkyCode = GetWeatherPic(tfm.img);
            //    foreach (ForecastImageComparer tf in fList)
            //        if (tf.dt >= dc && tf.dt < dc.AddDays(1))
            //        {
            //            if (fd.HighTemperature < tf.temp) fd.HighTemperature = tf.temp;
            //            if (fd.LowTemperature > tf.temp) fd.LowTemperature = tf.temp;
            //            fd.Url = "http://www.gismeteo.ru/city/hourly/" + rePrefix.Match(location.Code).Groups[1].Value + "/" +
            //                (dc > DateTime.Today.AddDays(1) ? "2/#wdaily" + ((dc - DateTime.Today).Days - 1).ToString() : "#wdaily" + ((dc - DateTime.Today).Days + 1).ToString());
            //            //http://www.gismeteo.ru/city/hourly/12980/2/#wdaily2

            //            foreach (ForecastImageComparer tfm in fListMain)
            //            {
            //                if (tf.dt >= tfm.dt && tf.dt < tfm.dt.AddDays(1) && tfm.CompareImg(tf.img))
            //                {
            //                    fd.SkyCode = GetWeatherPic(tfm.img);
            //                    fd.Text = GetWeatherDesc(tfm.img, culture) != String.Empty ? GetWeatherDesc(tfm.img, culture) : tf.desc;
            //                }
            //            }
            //            if (String.IsNullOrEmpty(fd.Text)) fd.Text = GetWeatherDescLng(tf.img, new CultureInfo("ru-RU"));
            //            //if (dc.AddHours(13) <= tf.dt && dc.AddHours(16) > tf.dt)
            //            //{
            //            //    fd.SkyCode = GetWeatherPic(tf.img);
            //            //    fd.Text = GetWeatherDesc(tf.img, culture) != String.Empty ? GetWeatherDesc(tf.img, culture) : tf.desc;
            //            //}
            //        }
            //    if (fd.HighTemperature != Int32.MinValue && fd.LowTemperature != Int32.MaxValue)
            //        result.ForecastList.Add(fd);
            //}

        }
    }
}
